#!/bin/bash

#History
#####################################################
#V1.0 initial vertion yinlijun@topwise3g.com
#####################################################


#NOTICE 
#this file will parse integration config file and do the all compiling step
#the config file script showing below

######################################################
#project_path=/home/yinlijun/project/a23gms #工程目录,必须是全目录
#branch=release #分支名称
#customer=yanghua #客户名称
#order=3665_YH-G739-VP74-A_14040385 #订单名称
######################################################

######################################################
#project_path=/home/yinlijun/project/a33 #工程目录
#branch=master_v2.0 #分支名称
#customer=topwise #客户名称
#order=wifionly #订单名称
######################################################

#Save last four line to a config file to integration.cfg
#You must remember the path, because the usage command line needs it

#Usage:
#./integration.sh ~/integration.cfg -c -s

function lichee_clean()
{
    local ori_dir=`pwd`
    local linux_path=`ls "$1" | grep "linux"`
    cd "$1/$linux_path"
    make clean
    cd ..
    #rm out -rf
    cd $ori_dir
}

function android_clean()
{
    rm "$1/out" -rf
}

function checkout_branch()
{
    repo forall -c 'pwd; git checkout -b '$2' '$1'/'$2'; git checkout '$2'; git pull'
}

function clean_branch()
{
    repo forall -c 'pwd; git reset HEAD --hard; git clean -fd; git checkout .;'
}

function delete_branch()
{
    repo forall -c 'pwd; git branch -D '$1''
}

function switch_branch()
{
   clean_branch
   checkout_branch $1 $2 
}

function pull_code()
{
    local remote_branch
    if [ $2 = "android" ]; then
        remote_branch="exdroid"
    else
        remote_branch="lichee"
    fi
    local ori_dir=`pwd`
    cd "$1/$2"
    
    if [ "$SYNC_CODE" = "1" ]; then
        switch_branch $remote_branch "master"
        delete_branch $BRANCH
        repo sync
    fi
    switch_branch $remote_branch $BRANCH
    delete_branch "master"
    
    cd $ori_dir
}

function pull_project_code()
{
    pull_code $1 "android"
    pull_code $1 "lichee"
}

function clean_project()
{
    lichee_clean "$1/lichee"
    android_clean "$1/android"
}

function set_env()
{
   PROJECT_PATH=$1
   BRANCH=$2
   CUSTOMER=$3
   ORDER=$4

   echo "PROJECT_PATH=$PROJECT_PATH"
   echo "BRANCH      =$BRANCH"
   echo "CUSTOMER    =$CUSTOMER"
   echo "ORDER       =$ORDER"
}

function unset_env
{
   unset PROJECT_PATH
   unset BRANCH
   unset CUSTOMER
   unset ORDER
}

function start_integration()
{
    local project_path=""
    local branch=""
    local customer=""
    local order=""
    local env_num=0


    for line in `cat $1`
    do
        if [ ${line%%=*} = "project_path" ]; then
            project_path=$(echo ${line##project_path=})
            let env_num+=1
        fi
        if [ ${line%%=*} = "branch" ]; then
            branch=$(echo ${line##branch=})
            let env_num+=1
        fi
        if [ ${line%%=*} = "customer" ]; then
            customer=$(echo ${line##customer=})
            let env_num+=1
        fi
        if [ ${line%%=*} = "order" ]; then
            order=$(echo ${line##order=})
            let env_num+=1
        fi
        if [ $env_num = 4 ]; then
           set_env $project_path $branch $customer $order
           build_project
           if [ $? = 1 ]; then
               break
           else 
               unset_env
               env_num=0
           fi
        fi
    done
}

function build_project()
{
    local ori_dir=`pwd`
    if [ "$CLEAN_PROJECT" = "1" ]; then
        clean_project $PROJECT_PATH
    fi
    pull_project_code $PROJECT_PATH
    cd "$PROJECT_PATH/android"
    source Topwisetools.sh
    if [ $CUSTOMER = "topwise" ]; then
        build_product $ORDER
    else
        build_custom all -c $CUSTOMER -o $ORDER
    fi

    
    return $?
}

function show_helps()
{
    printf " 用法: integration.sh integration.cfg 选项:
    例如 ~/integration.sh ~/integration.cfg -c -s
    编译软件

    选项:
    -c     清空编译 
    -s     同步代码
"
}

CLEAN_PROJECT=0
SYNC_CODE=

OPTIND=2
OPTION=
while getopts "cs" OPTION
do
    case $OPTION in
        c) CLEAN_PROJECT=1
            ;;
        s) SYNC_CODE=1
            ;;
        *) show_helps
            ;;
    esac
done
unset OPTION

start_integration $1

